package com.myapp.foodapp;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.myapp.foodapp.net.NetUtil;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Date;


public class CommentsAddActivity extends AppCompatActivity {
    private static final String TAG = "CommentsAddActivit";
    EditText mEtComments;
    Button mBtnAdd;
    int userId;
    int foodId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comments_add);
        initView();
        initData();
        initEvent();
    }

    private void initView() {
        mEtComments = findViewById(R.id.et_comments);
        mBtnAdd = findViewById(R.id.btn_add);
    }

    private void initData() {
        //初始化数据，从Intent中获取suerid的值
//        userId = getIntent().getIntExtra("userId",-1);
        SharedPreferences sp = getSharedPreferences("data.sp", MODE_PRIVATE);
        userId = sp.getInt("userId", -1);
        foodId = getIntent().getIntExtra("foodId", -1);


    }

    private void initEvent() {
        mBtnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //访问网络，提交评论
                //准备url，jsonobject
                final String comment = mEtComments.getText().toString();//获取用户输入的评论信息
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        JSONObject jsonObject = new JSONObject();
                        //通过new Date（）获取当前的系统时间，然后利用 SimpleDateFormat 转换
                        // 为目标格式字符串
                        Date date = new Date();
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                        String postDate = sdf.format(date);
                        Log.e(TAG, "run:" + postDate);
                        //userId
                        Log.e(TAG, "run" + userId);
                        //foodId
                        Log.e(TAG, "run" + foodId);
                        try {
                            jsonObject.put("comment", comment);
                            jsonObject.put("userId", userId);
                            jsonObject.put("foodId", foodId);
                            jsonObject.put("postDate", postDate);


                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        String url = "http://10.2.64.75:8080/foodserver/comments/add";
                        String data = NetUtil.getData(jsonObject,url);
                        //使用Gson
                        Gson gson = new Gson();
                      CommentsAddBean commentsAddBean =  gson.fromJson(data,CommentsAddBean.class);
                      if(commentsAddBean.isSuccess()){
                          Log.e(TAG, "run" + "成功");
                          runOnUiThread(new Runnable() {
                              @Override
                              public void run() {
                                  Toast.makeText(CommentsAddActivity.this, "发表成功了", Toast.LENGTH_SHORT).show();
                              }
                          });
                      }else {
                          Log.e(TAG, "run" + "失败");
                          runOnUiThread(new Runnable() {
                              @Override
                              public void run() {
                                  Toast.makeText(CommentsAddActivity.this, "发表失败了", Toast.LENGTH_SHORT).show();
                              }
                          });
                      }

                    }
                }).start();
            }
        });
    }
}